package com.os.sys.controller;

import com.os.sys.config.MyProps;
import com.os.sys.mapper.RoleMenuMapper;
import com.os.sys.mapper.SysMenuMapper;
import com.os.sys.mapper.SysRoleMapper;
import com.os.sys.pojo.SysMenu;
import com.os.sys.pojo.SysRole;
import com.os.sys.service.SysMenuService;
import com.os.sys.service.SysRoleService;
import com.os.sys.service.SysUserService;
import com.os.sys.service.bean.MenuBean;
import com.os.sys.service.bean.ZTreeNode;
import entity.Result;
import entity.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import util.JwtUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/menu")
@RefreshScope
@Slf4j
public class SysMenuController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private MyProps myProps;
    //https://www.cnblogs.com/superfj/p/9232482.html
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    HttpServletRequest request;
    @Autowired
    SysMenuMapper sysMenuMapper ;
    @Autowired
    RoleMenuMapper roleMenuMapper ;
    @Autowired
    SysRoleMapper sysRoleMapper ;

    @RequestMapping(value="/doSelBean", method= RequestMethod.GET)
    @ResponseBody
    public Result doSelBean(int menu_id) {
        MenuBean menuBean = sysMenuMapper.selectBeanById(menu_id);
        List list = new ArrayList();
        list.add(menuBean);
        return new Result(true, StatusCode.OK,"查询成功", menuBean);
    }

    @RequestMapping(value="/doSelNode", method= RequestMethod.POST)
    @ResponseBody
    public List<ZTreeNode> doSelNode(String id, String id_ini, String role_id) {
        //SysMenuDao sysMenuDao = SysDaoFactory.getSysMenuDao();
        //SysRoleDao sysRoleDao = SysDaoFactory.getSysRoleDao();
        //RoleMenuDao roleMenuDao = SysDaoFactory.getRoleMenuDao();

        //String id = request.getParameter("id");
        //String id_ini = request.getParameter("id_ini");

        log.info(" id = "+id) ;
        if(id==null || id.length()==0){
            id = id_ini ;
        }

        if(id==null || id.length()==0){
            id = "-1";
        }
        //id = "-1";
        List<ZTreeNode> treeNodeList = new ArrayList();
        String menu_ids_fin = " ,"+sysMenuService.getMenu_ids_fin()+",";
        log.debug("menu_ids_fin = "+menu_ids_fin);
        if(menu_ids_fin.indexOf(","+id+",")>=0){
            //String jsonStr1 = this.toJsonString(treeNodeList);
            //response.getWriter().print(jsonStr1);
            return treeNodeList;
        }
        List<SysMenu> sysMenuList = sysMenuMapper.selectChildmenu(Integer.valueOf(id));
        //String role_id = request.getParameter("role_id");
        String menu_id_inrole = " ,";
        Map<Integer,String> oldCheckedMenuMap = new HashMap();
        //System.out.println("role_id = "+role_id);
        if(role_id!=null && !"undefined".equals(role_id)){
            int role = Integer.valueOf(role_id);
            SysRole sysRole = sysRoleMapper.selectById(role);
            List<SysRole> list = new ArrayList();
            list.add(sysRole);
            List<Integer> idList = new ArrayList();
            idList.add(role);
//            String role_ids = ","+Tools.getIdsFromList(list,"role_id")+",";
//            log.info("role_ids=", role_ids);
            List<SysMenu> menuList = roleMenuMapper.selectSysMenuByRoleIds(idList);
            for(int i=0; i<menuList.size(); i++){
                int menu_id_temp = menuList.get(i).getMenu_id();
                menu_id_inrole = menu_id_inrole + menu_id_temp + ",";
                oldCheckedMenuMap.put(menuList.get(i).getMenu_id(), menuList.get(i).getOper());
            }

        }
        for(int i=0; i<sysMenuList.size(); i++){
            SysMenu sysMenu = sysMenuList.get(i);
            int menu_id = sysMenu.getMenu_id();
            String menu_name = sysMenu.getMenu_name();
            ZTreeNode node = new ZTreeNode();
            node.setId(menu_id+"");
            node.setName(menu_name);
            //node.setChecked("true");
            if(role_id!=null){
                if(menu_id_inrole.indexOf(","+menu_id+",")>=0){
                    node.setChecked("true");
                    node.setOper(oldCheckedMenuMap.get(menu_id));
                }
            }
            treeNodeList.add(node);
        }
        return treeNodeList;
        //String jsonStr = this.toJsonString(treeNodeList);
        //response.getWriter().print(jsonStr);
    }
    @RequestMapping(value="/add",method = RequestMethod.POST)
    public Result add(@RequestBody SysMenu sysMenu){
        sysMenuService.insert(sysMenu);
        log.info("sysMenu = ", sysMenu);
        return new Result(true, StatusCode.OK,"添加成功", sysMenu);
    }
    @RequestMapping(value="/update",method = RequestMethod.POST)
    public Result update(@RequestBody SysMenu sysMenu){
        sysMenuService.update(sysMenu);
        return new Result(true, StatusCode.OK,"修改成功", sysMenu);
    }
    @RequestMapping(value="/{menu_id}",method = RequestMethod.DELETE)
    public Result delete(@PathVariable int menu_id){
        sysMenuService.deleteById(menu_id);
        return new Result(true, StatusCode.OK,"删除成功");
    }
    @RequestMapping(value="/genCode",method = RequestMethod.GET)
    public Result genCode(String menu_code_parent, int menu_id_parent){
        String code = sysMenuService.genCode( menu_code_parent,  menu_id_parent);
        return new Result(true, StatusCode.OK,"修改成功", code);
    }
}
